package com.mall.service.impl;

import com.mall.entity.MmallProduct;
import com.mall.mapper.MmallProductMapper;
import com.mall.service.MmallProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ppmall
 * @since 2021-09-19
 */
@Service
public class MmallProductServiceImpl extends ServiceImpl<MmallProductMapper, MmallProduct> implements MmallProductService {

    @Autowired
    RedisUtil redisUtil;

    @Override
    public MmallProduct getProductById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public boolean updateProductById(MmallProduct product) {
        int update = baseMapper.updateById(product);
        if(update > 0) {
            String redis_cache_name = "product" + product.getId();
            redisUtil.del(redis_cache_name);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean saveProduct(MmallProduct product) {
        int insert = baseMapper.insert(product);
        if(insert > 0) {
            String redis_cache_name = "product" + product.getId();
            redisUtil.del(redis_cache_name);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean removeProductById(Integer id) {
        int delete = baseMapper.deleteById(id);
        if(delete > 0) {
            String redis_cache_name = "product" + id;
            redisUtil.del(redis_cache_name);
            return true;
        } else {
            return false;
        }
    }


}
